package com.faintlight.controller;

import com.faintlight.model.Affirmation;
import com.faintlight.service.AffirmationService;
import com.faintlight.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/affirmations")
@Tag(name = "积极肯定语", description = "积极肯定语管理接口")
@RequiredArgsConstructor
public class AffirmationController {

    private static final Logger logger = LoggerFactory.getLogger(AffirmationController.class);

    private final AffirmationService affirmationService;
    private final UserService userService;

    @PostMapping
    @Operation(summary = "保存新的肯定语")
    public ResponseEntity<Affirmation> saveAffirmation(@RequestBody Affirmation affirmation, @RequestHeader("X-User-Id") Long userId) {
        affirmation.setUser(userService.getUserById(userId));
        Affirmation saved = affirmationService.saveAffirmation(affirmation);
        return ResponseEntity.ok(saved);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户的肯定语")
    public ResponseEntity<List<Affirmation>> getUserAffirmations(@PathVariable Long userId) {
        List<Affirmation> affirmations = affirmationService.getUserAffirmations(userId);
        return ResponseEntity.ok(affirmations);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除肯定语")
    public ResponseEntity<Map<String, String>> deleteAffirmation(@PathVariable Long id, @RequestHeader("X-User-Id") Long userId) {
        affirmationService.deleteAffirmation(id, userId);
        Map<String, String> response = new HashMap<>();
        response.put("message", "肯定语删除成功");
        return ResponseEntity.ok(response);
    }
}